package fr.agriparcelle.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * 
 * @author Parant
 * 
 */
public class Agriparcelle implements EntryPoint {

	// private final CredentialsServiceAsync credentialsService =
	// GWT.create(CredentialsService.class);

	public void onModuleLoad() {
		final VLayout layout = new VLayout();
		layout.setWidth100();
		layout.setHeight100();

		final TabSet mainTabSet = new TabSet();
		mainTabSet.setTabBarPosition(Side.TOP);
		mainTabSet.setWidth100();
		mainTabSet.setWidth100();

		Tab dashBoardTab = new Tab("Dashboard");
		dashBoardTab.setPane(new VLayout());

		Tab mySettings = new Tab("My settings");
		mySettings.setPane(new VLayout());

		Tab adminTab = new Tab("Admin");
		adminTab.setPane(createDashBoardLayout());

		mainTabSet.addTab(dashBoardTab);
		mainTabSet.addTab(mySettings);
		mainTabSet.addTab(adminTab);
		layout.addMember(mainTabSet);

		layout.draw();
	}

	private Canvas createDashBoardLayout() {
		TreeGrid treeGrid = new TreeGrid();
		treeGrid.setWidth(300);
		treeGrid.setHeight(400);

		TreeGridField field = new TreeGridField("firstName", "User list");
		field.setCanSort(false);

		treeGrid.setFields(field);

		final Tree tree = new Tree();
		tree.setModelType(TreeModelType.PARENT);
		tree.setIdField("firstName");
		tree.setParentIdField("group");
		tree.setShowRoot(true);

		GroupTreeNode root = new GroupTreeNode("Admin");
		UserTreeNode node1 = new UserTreeNode("Pascal", "Pascal", "Parant", "Admin");
		UserTreeNode node2 = new UserTreeNode("Hubert", "Hubert", "Terrier", "Admin");
		tree.setData(new TreeNode[] { root, node1, node2 });

		treeGrid.addDrawHandler(new DrawHandler() {
			public void onDraw(DrawEvent event) {
				tree.openAll();
			}
		});

		treeGrid.setData(tree);

		treeGrid.draw();

		return treeGrid;
	}

	public static class GroupTreeNode extends TreeNode {

		public GroupTreeNode(String name) {
			setName(name);
		}

		public void setName(String value) {
			setAttribute("firstName", value);
		}
	}

	public static class UserTreeNode extends TreeNode {

		public UserTreeNode(String id, String firstName, String lastName, String group) {
			setFirstName(firstName);
			setLastName(lastName);
			setGroup(group);
		}

		private void setGroup(String value) {
			setAttribute("group", value);
		}

		public void setFirstName(String value) {
			setAttribute("firstName", value);
		}

		public void setLastName(String name) {
			setAttribute("lastName", name);
		}
	}
}
